<?php

namespace Rblog\Module\Admin\Controller;

class Userlist extends Index {
    
    public function __construct() {
        $this->_working = 'userlist';
        parent::__construct();
    }
    
    /**
     * 用户列表
     */
    public function show() {
        $userData = \Rexzy::model('User');
        $length = 8;
        $condition = array();
        
        // 获取列表信息
        if ( $this->request->input('page', 'g' ) != false ) {
            $page = $this->request->input('page', 'g' );
        } else {
            $page = 1;
        }
        
        $start = ( $page - 1 ) * $length;
        if ( $this->request->isPost() ) {
            $condition['seach'] = $this->request->input('seach');
        } else if ( $this->request->input('seach', 'g' ) != false ) {
            $condition['seach'] = $this->request->input('seach', 'g' );
        }
        $total = count( $userData->getList( $condition ) );
        $userList = $userData->getList( $condition, $start, $length );
        
        $keyword = '任何';
        if ( count( $condition ) != 0 ) {
            $returnHome = true;
            $keyword = "包含关键字 '" . $condition['seach']. "' 的";
        } else {
            $returnHome = false;
        }
        
        // 整理分页信息
        $args = array(
            'rang' => 5,
            'length' => $length,
            'total' => $total,
            'page' => $page,
            'url' => \Rexzy::url('admin/userlist'),
            'condition' => $condition,
            'userList' => $userList,
            'editurl' => \Rexzy::url('admin/useredit'),
            'keyword' => $keyword,
            'returnHome' => $returnHome
            );
        
        $this->view->assign( $args );
        $this->view->display();
    }
    
    /**
     * 删除用户动作（用户列表的附加动作）
     * 
     * @return boolean 删除结果
     */
    public function delete() {
        $postVal = $this->request->input( 'value', 'p');
        $userid = explode( ',', $postVal );
        $userLib = \Rexzy::model( 'User' );
        
        $result = $userLib->delUser( $userid );
        
        if ( $result > 0 ) {
            echo json_encode( array( 'delresult' => true ) );
        } else {
            echo json_encode( array( 'delresult' => false ) );
        }
    }
    
    /**
     * 更改用户资料
     * 
     * @return boolean 更改结果
     */
    public function edit() {
        if( ( $this->request->isGet() == true ) ) {
            //进入编辑表单
            $userid = $this->request->input('userid', 'g');
            $user = \Rexzy::model( 'User' );
            
            $userMesg = $user->get( $userid );
            if ( $userMesg['userid'] == $this->_currentUserid ){
                $this->_working = 'current-user';
            }
            
            foreach( $userMesg as $key => $value ) {
                $this->view->assign( $key, $value );
            }
            $this->view->assign( 'work', $this->_working );
            $this->view->display();
            
        } else if ($this->request->isPost() == true ) {
            //提交编辑后的内容进行更改
            $post = $this->request->allInput( 'p' );
            $postid = $post['userid'];
            
            //确认密码是否正确，是否允许修改资料
            $user = \Rexzy::model( 'User' );
            $where = "where userid = " . $post['userid'];
            $filed = '*';
            $result = $user->select( $filed, $where );
         
            if ( $post['oldpaswd'] == '' ) {
                echo json_encode( array( 'oldpaswd' => array( 'id' => 'oldpaswd',
                    'result' => false, 'message' => '请输入原用户密码' ) ) );
                return false;
            } else if ( md5( $post['oldpaswd'] . \Rexzy::getConfig( 'slat' )) != $result['password'] ) {
                echo json_encode( array( 'oldpaswd' => array( 'id' => 'oldpaswd',
                    'result' => false, 'message' => '输入的原用户密码不对' ) ) );
                return false;
            }
            
            //先判断是否有变更的内容
            if ( $post['screenName'] == '' ) {
                $screenNameVal = $post['name'];
            } else {
                $screenNameVal = $post['screenName'];
            }
            
            if ( ($result['name'] == $post['name']) && ($result['screenName'] == 
                    $screenNameVal) && ($result['usergroup'] == $post['group']) 
                    && ($result['email'] == $post['email']) && ( ( $post['newpaswd'] == '' )
                    || ( md5( $post['newpaswd'] . \Rexzy::getConfig( 'slat' )) == 
                    $result['password'] ) ) ) {
                echo json_encode( array('otherError' => '没有要更新的内容') );
                return false;
            }
            
            if ( $post['newpaswd'] != $post['confNewPaswd'] ) {
                echo json_encode( array( 'confNewPaswd' => array( 'id' => 'confNewPaswd', 'result' => false, 'message' => '密码不一致' ) ) );
                return false;
            }
            
            //整理要修改的信息
            $name = array(
                'val' => $post['name'],
                'message' => '用户名不能为空',
                'rules' => array(
                    array(
                      'rule' => function($val) {
                            if ( ( strlen($val) < 4 ) || ( strlen($val) > 16 ) ){
                                return false;
                            } 
                        },
                        'error' => '请输入4-16个字符的用户名'  
                    ),
                    array(
                        'rule' => function($val)use( $postid,$user) {
                            $userid = $user->userid($val);
                            if ( ( $userid != '' ) && ( $userid != $postid) ) {
                                return false;
                            }
                        },
                        'error' => '用户名已存在'
                    )
                )
            );
                    
            $newpaswd = array(
                'val' => $post['newpaswd'],
                'require' => false,
                'rules' => array(
                    array(
                        'rule' => function($val) {
                            if ( ( strlen($val) < 6 ) || ( strlen($val) > 20 ) ){
                                return false;
                            } 
                        },
                        'error' => '请输入6-20个字符的密码'  
                    )
                )
            );
            
            $confNewPaswd = array(
                'val' => $post['confNewPaswd'],
                'require' => false
            );
                
            $screenName = array(
                'val' => $screenNameVal,
                'require' => false
            );
            
            $email = array(
                'val' => $post['email'],
                'require' => false
            );
            
            $group = array(
                'val' => $post['group'],
                'require' => true,
                'rules' => array(
                    array(
                        'rule' => function($val) {
                            $value = array( 'subscriber', "contributor", "editor", "administrator");
                            if( ! in_array( $val, $value) ) {
                                return false;
                            }
                        },
                        'error' => '请根据给出的选项选择用户组'
                    )
                ) 
            );
                        
            $data = array(
                'name'=> $name,
                'newpaswd' => $newpaswd,
                'confNewPaswd' => $confNewPaswd,
                'screenName' => $screenName,
                'email' => $email,
                'usergroup' => $group
            );
            
            $rightHandle = function( $rightData )use($user, $postid) {
                $where = array( 'userid' => $postid );
                $updateVal = array();
                
                foreach( $rightData as $key => $value ) {
                    if ( $value !== '' ) {
                        if ( $key == 'newpaswd' ) {
                            $updateVal['password'] = md5( $value . \Rexzy::getConfig( 'slat' ) );
                        } else if ( $key == 'confNewPaswd' ) {
                            continue;
                        } else {
                            $updateVal[ $key ] = $value;
                        }
                    } else {
                        continue;
                    }
                }
                
                return $user->update( $updateVal, $where );
            };
            
            echo json_encode( $this->formCheck( $data, $rightHandle ) ); 
        }
    }
    
}